﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using IndieStudio.BugsBang.Utility;
using IndieStudio.BugsBang.Game;

///Developed by Indie Studio
///https://assetstore.unity.com/publishers/9268
///www.indiestd.com
///info@indiestd.com

[CustomEditor (typeof(LevelsManager))]
public class levelsManagerEditor : Editor
{
	private static bool showInstructions = true;
	private static int selectedLevel;

	public override void OnInspectorGUI ()
	{
		LevelsManager levelsManager = (LevelsManager)target;//get the target

		EditorGUILayout.Separator ();
		#if !(UNITY_5 || UNITY_2017 || UNITY_2018_0 || UNITY_2018_1 || UNITY_2018_2)
			//Unity 2018.3 or higher
			EditorGUILayout.BeginHorizontal();
			GUI.backgroundColor = Colors.cyanColor;
			EditorGUILayout.Separator();
			if(PrefabUtility.GetCorrespondingObjectFromSource(levelsManager.gameObject)!=null)
			if (GUILayout.Button("Apply", GUILayout.Width(70), GUILayout.Height(30), GUILayout.ExpandWidth(false)))
			{
				PrefabUtility.ApplyPrefabInstance(levelsManager.gameObject, InteractionMode.AutomatedAction);
			}
			GUI.backgroundColor = Colors.whiteColor;
			EditorGUILayout.EndHorizontal();
		#endif
		EditorGUILayout.Separator ();
		
		EditorGUILayout.HelpBox ("Follow the instructions below on how to add new level", MessageType.None);

		showInstructions = EditorGUILayout.Foldout (showInstructions, "Instructions");
		EditorGUILayout.Separator ();

		if (showInstructions) {
			EditorGUILayout.HelpBox ("- Click on 'Add New Level' button to add new Level", MessageType.None);
			EditorGUILayout.HelpBox ("- Click on 'Remove Last Level' button to remove the lastest level in the list", MessageType.None);
			EditorGUILayout.HelpBox ("- Click on 'Add New Target' button to add new target or path object for the level", MessageType.None);
			EditorGUILayout.HelpBox ("- Click on 'Remove Last Target' button to remove the lastest target in the list", MessageType.None);
			EditorGUILayout.HelpBox ("- Click on 'Add New Path' button to add new path in the level", MessageType.None);
			EditorGUILayout.HelpBox ("- Click on 'Remove Last Path' button to remove the lastest path in the list", MessageType.None);

			EditorGUILayout.HelpBox ("* Enable the Test Mode flag to enable the test mode => All Levels will be unlocked for testing", MessageType.None);
			EditorGUILayout.HelpBox ("* Click on 'Apply' button that located at the top to save your changes", MessageType.None);
			EditorGUILayout.HelpBox ("* Run the game from the Main scene", MessageType.None);
		}

		EditorGUILayout.Separator ();
		levelsManager.testMode = EditorGUILayout.Toggle ("Test Mode", levelsManager.testMode);
		EditorGUILayout.Separator ();

		GUILayout.BeginHorizontal ();
		GUI.backgroundColor = Colors.greenColor;         

		if (GUILayout.Button ("Add New Level", GUILayout.Width (110), GUILayout.Height (20))) {
			levelsManager.levels.Add (new LevelsManager.Level ());
			selectedLevel =	levelsManager.levels.Count - 1;
		}

		GUI.backgroundColor = Colors.redColor;         
		if (GUILayout.Button ("Remove Last Level", GUILayout.Width (150), GUILayout.Height (20))) {
			if (levelsManager.levels.Count != 0) {
				levelsManager.levels.RemoveAt (levelsManager.levels.Count - 1);
			}
		}

		GUI.backgroundColor = Colors.whiteColor;
		GUILayout.EndHorizontal ();

		EditorGUILayout.Separator ();

		if (levelsManager.levels.Count == 0) {
			EditorGUILayout.LabelField ("There are no levels found");
			return;
		}

		GUILayout.Box ("Levels Section", GUILayout.ExpandWidth (true), GUILayout.Height (22));
		EditorGUILayout.HelpBox ("Move between levels using Selected Level slider", MessageType.Info);
		EditorGUILayout.Separator ();

		EditorGUILayout.LabelField ("Number of Levels : " + levelsManager.levels.Count);

		EditorGUILayout.Separator ();
		EditorGUILayout.BeginHorizontal ();
		if (GUILayout.Button ("<", GUILayout.Width (20), GUILayout.Height (28))) {
			if (selectedLevel - 1 >= 0)
				selectedLevel -= 1;
		}

		selectedLevel = selectedLevel + 1;

		EditorGUILayout.BeginVertical ();
		GUILayout.Space (10);
		selectedLevel = EditorGUILayout.IntSlider ("Selected Level", selectedLevel, 1, levelsManager.levels.Count); 
		EditorGUILayout.EndVertical ();

		selectedLevel = selectedLevel - 1;

		if (GUILayout.Button (">", GUILayout.Width (20), GUILayout.Height (28))) {
			if (selectedLevel + 1 < levelsManager.levels.Count)
				selectedLevel += 1;
		}

		EditorGUILayout.EndHorizontal ();

		EditorGUILayout.Separator ();
		EditorGUILayout.Separator ();

		//Draw the selected level
	
		EditorGUILayout.BeginHorizontal ();
		EditorGUILayout.BeginVertical ();

		EditorGUILayout.BeginVertical ("Box");

		//Draw level title
		GUI.backgroundColor = Colors.transparent;   
		GUI.skin.box.fontStyle = FontStyle.Bold;
		GUILayout.Box ("Level " + (selectedLevel + 1), GUILayout.ExpandWidth (true), GUILayout.Height (20));
		GUI.skin.box.fontStyle = FontStyle.Normal;
		GUI.backgroundColor = Colors.whiteColor;
		EditorGUILayout.Separator ();

		EditorGUILayout.BeginHorizontal ();
		GUILayout.Space (10);
		EditorGUILayout.BeginVertical ();
		GUILayout.Space (10);

		levelsManager.levels [selectedLevel].randomMode = EditorGUILayout.Toggle ("Random Mode",levelsManager.levels [selectedLevel].randomMode); 
		EditorGUILayout.Separator ();
		levelsManager.levels [selectedLevel].starsTimePeriod = EditorGUILayout.IntSlider ("Stars Period(s)",levelsManager.levels [selectedLevel].starsTimePeriod,0,500); 
		EditorGUILayout.Separator ();
		levelsManager.levels [selectedLevel].lives = EditorGUILayout.IntSlider ("Number of Lives",levelsManager.levels [selectedLevel].lives,1,100); 
		EditorGUILayout.Separator ();

		//Draw Add , Remove buttons for the target
		GUILayout.BeginHorizontal ();
		GUI.backgroundColor = Colors.paleGreen;         
		if (GUILayout.Button ("Add New Target", GUILayout.Width (110), GUILayout.Height (20))) {
			levelsManager.levels [selectedLevel].targets.Add (new LevelsManager.Level.Target ());
		}

		GUI.backgroundColor = Colors.redColor;         
		if (GUILayout.Button ("Remove Last Target", GUILayout.Width (130), GUILayout.Height (20))) {
			if (levelsManager.levels [selectedLevel].targets.Count != 0) {
				levelsManager.levels [selectedLevel].targets.RemoveAt (levelsManager.levels [selectedLevel].targets.Count - 1);
			}
		}
		GUI.backgroundColor = Colors.whiteColor;

		GUILayout.EndHorizontal ();

		EditorGUILayout.Separator ();

		//Draw the targets of the level
		for (int j = 0; j < levelsManager.levels [selectedLevel].targets.Count; j++) {
								
			levelsManager.levels [selectedLevel].targets [j].showContents = EditorGUILayout.Foldout (levelsManager.levels [selectedLevel].targets [j].showContents, "Target[" + j + "]");
			if (levelsManager.levels [selectedLevel].targets [j].showContents) {
				levelsManager.levels [selectedLevel].targets [j].pathObject = EditorGUILayout.ObjectField ("Path Object",	levelsManager.levels [selectedLevel].targets [j].pathObject, typeof(PathObject), true) as PathObject;
				levelsManager.levels [selectedLevel].targets [j].count = EditorGUILayout.IntSlider ("Count", levelsManager.levels [selectedLevel].targets [j].count, 1, 99);
			}
		}

		EditorGUILayout.Separator ();
		GUILayout.Box ("", GUILayout.ExpandWidth (true), GUILayout.Height (2));
		EditorGUILayout.Separator ();

		//Draw Add , Remove buttons for the path
		GUILayout.BeginHorizontal ();
		GUI.backgroundColor = Colors.paleGreen;         
		if (GUILayout.Button ("Add New Path", GUILayout.Width (110), GUILayout.Height (20))) {
			levelsManager.levels [selectedLevel].paths.Add (new LevelsManager.Level.Path ());
		}
		GUI.backgroundColor = Colors.redColor;         
		if (GUILayout.Button ("Remove Last Path", GUILayout.Width (130), GUILayout.Height (20))) {
			if (levelsManager.levels [selectedLevel].paths.Count != 0) {
				levelsManager.levels [selectedLevel].paths.RemoveAt (levelsManager.levels [selectedLevel].paths.Count - 1);
			}
		}
		GUI.backgroundColor = Colors.whiteColor;         
		GUILayout.EndHorizontal ();

		EditorGUILayout.Separator ();

		//Draw the paths of the level
		for (int j = 0; j < levelsManager.levels [selectedLevel].paths.Count; j++) {
			levelsManager.levels [selectedLevel].paths [j].showContents = EditorGUILayout.Foldout (levelsManager.levels [selectedLevel].paths [j].showContents, "Path[" + j + "]");
			if (levelsManager.levels [selectedLevel].paths [j].showContents) {
				levelsManager.levels [selectedLevel].paths [j].value = EditorGUILayout.ObjectField ("Reference", levelsManager.levels [selectedLevel].paths [j].value, typeof(Path), true) as Path;

				//Draw Add , Remove buttons for the path object of the current path
				GUILayout.BeginHorizontal ();
				if (GUILayout.Button ("Add New Object", GUILayout.Width (110), GUILayout.Height (20))) {
					levelsManager.levels [selectedLevel].paths [j].pathObjects.Add (null);
				}
				if (GUILayout.Button ("Remove Last Object", GUILayout.Width (130), GUILayout.Height (20))) {
					if (levelsManager.levels [selectedLevel].paths [j].pathObjects.Count != 0) {
						levelsManager.levels [selectedLevel].paths [j].pathObjects.RemoveAt (levelsManager.levels [selectedLevel].paths [j].pathObjects.Count - 1);
					}
				}
				GUILayout.EndHorizontal ();

				//Draw path objects of the current path
				for (int k = 0; k < levelsManager.levels [selectedLevel].paths [j].pathObjects.Count; k++) {
					levelsManager.levels [selectedLevel].paths [j].pathObjects [k] = EditorGUILayout.ObjectField ("Path Object",	levelsManager.levels [selectedLevel].paths [j].pathObjects [k], typeof(PathObject), true) as PathObject;
				}
				EditorGUILayout.Separator ();
				EditorGUILayout.Separator ();
			}
		}
					
		EditorGUILayout.Separator ();
		EditorGUILayout.Separator ();

		EditorGUILayout.EndVertical ();
		EditorGUILayout.EndHorizontal ();

		EditorGUILayout.EndVertical ();

		EditorGUILayout.EndVertical ();
		EditorGUILayout.EndHorizontal ();

		if (GUI.changed) {
			DirtyUtil.MarkSceneDirty ();
		}
	}
}